clear all; close all; clc;
rng('shuffle')

N = 100000;

% uniform

a = 10; b = 22;
x = a + (b-a)*rand(1, N);

bins = (a-2):0.1:(b+2)
[yh, xh] = hist(x, bins);

figure;
hist(x, bins);
hold on
plot(xh, yh, 'r', 'LineWidth', 3)
hold off

figure;
dxh = xh(2) - xh(1);
ph = yh / ( sum(yh)*dxh );

plot(xh, ph);
xlabel('X'); ylabel('P(X)'); 
ylim([0 1.1*max(ph)]);

% gauss
sko = 2; m = 16;
x = m + sko*randn(1, N);

bins = (m-4*sko):(sko/4):(m+4*sko);
[yh, xh] = hist(x, bins);

figure;
hist(x, bins);
hold on
plot(xh, yh, 'r', 'LineWidth', 3)
hold off

figure;
dxh = xh(2) - xh(1);
ph = yh / ( sum(yh)*dxh );

plot(xh, ph);
xlabel('X'); ylabel('P(X)'); 
ylim([0 1.1*max(ph)]);

% chi2
sko = 2; m = 4;
x1 = m + sko*randn(1, N);
x2 = m + sko*randn(1, N);

x = x1.^2 + x2.^2;

[nul, bins] = hist(x, 20);
[yh, xh] = hist(x, bins);

figure;
hist(x, bins);
hold on
plot(xh, yh, 'r', 'LineWidth', 3)
hold off

figure;
dxh = xh(2) - xh(1);
ph = yh / ( sum(yh)*dxh );

plot(xh, ph);
xlabel('X'); ylabel('P(X)'); 
ylim([0 1.1*max(ph)]);

% reley
sko = 2; m = 0;
x1 = m + sko*randn(1, N);
x2 = m + sko*randn(1, N);

x = sqrt(x1.^2 + x2.^2);

[nul, bins] = hist(x, 20);
[yh, xh] = hist(x, bins);

figure;
hist(x, bins);
hold on
plot(xh, yh, 'r', 'LineWidth', 3)
hold off

figure;
dxh = xh(2) - xh(1);
ph = yh / ( sum(yh)*dxh );

plot(xh, ph);
xlabel('X'); ylabel('P(X)'); 
ylim([0 1.1*max(ph)]);

% rice
sko = 2; m = 10;
x1 = m + sko*randn(1, N);
x2 = m + sko*randn(1, N);

x = sqrt(x1.^2 + x2.^2);

[nul, bins] = hist(x, 20);
[yh, xh] = hist(x, bins);

figure;
hist(x, bins);
hold on
plot(xh, yh, 'r', 'LineWidth', 3)
hold off

figure;
dxh = xh(2) - xh(1);
ph = yh / ( sum(yh)*dxh );

plot(xh, ph);
xlabel('X'); ylabel('P(X)'); 
ylim([0 1.1*max(ph)]);1

